import {
  mock,
} from '../base/index.js'
import request from '@/utils/request/index.js'
import { fmsPath } from '@/configs/index'
import store from '@/store/index.js'

const Api = {
  Login: '/member/login',
  LoginByEmail: '/member/login_by_email',
  LoginBySms: '/member/login_by_sms',
  LoginByWechatMini: '/oauth/login/wechat/mini_program',
  Captcha: '/captcha',
  EmailCaptcha: '/captcha/email',
  SmsCaptcha: '/captcha/sms',
  UpdateProfile: '/member/profile',
  Logout: '/member/logout',
  Register: '/member/register',
  RegisterByEmail: '/member/register_by_email',
  RegisterBySms: '/member/register_by_sms',
  BindWeChat: '/member/bind/wechat',
}

/**
 * @description 用户登录
 */
export const memberLogin = params => request.post(Api.Login, params)

/**
 * @description 用户邮箱登录
 */
export const memberLoginByEmail = params => request.post(Api.LoginByEmail, params)

/**
 * @description 用户短信登录
 */
export const memberLoginBySms = params => request.post(Api.LoginBySms, params)

/**
 * @description 用户小程序登录
 */
export const memberLoginByWechatMini = params => request.post(Api.LoginByWechatMini, params)

/**
 * @description 用户小程序登录
 */
export const memberBindWechat = params => request.post(Api.BindWeChat, params)

/**
 * @description 用户注册
 */
export const memberRegister = params => request.post(Api.Register, params)

/**
 * @description 用户邮箱注册
 */
export const memberRegisterByEmail = params => request.post(Api.RegisterByEmail, params)

/**
 * @description 用户短信注册
 */
export const memberRegisterBySms = params => request.post(Api.RegisterBySms, params)

/**
 * @description 获取验证码
 */
export const getCaptcha = () => request.get(Api.Captcha)

/**
 * @description 获取邮箱验证码
 */
export const getEmailCaptcha = params => request.post(Api.EmailCaptcha, params)

/**
 * @description 获取短信验证码
 */
export const getSmsCaptcha = params => request.post(Api.SmsCaptcha, params)

/**
 * @description 用户修改密码
 */
export const changePassword = data => mock(data)

/**
 * @description 更新会员信息
 */
export const updateProfile = params => request.post(Api.UpdateProfile, params)

/**
 * @description 上传图片
 */
export const uploadImg = async params => new Promise((resolve, reject) => {
  const userStore = store.useUserStore()
  const token = userStore.token

  uni.uploadFile({
    url: `${fmsPath}/upload`,
    filePath: params,
    name: 'file',
    header: {
      accept: 'application/json',
      authorization: token,
    },
    success: (uploadFileRes) => {
      resolve(uploadFileRes.data)
    },
    fail: (e) => {
      reject(e)
    },
  })
})

/**
 * @description 退出登录
 */
export const logout = () => request.get(Api.Logout)
